<?php //echo $this->iduser ;?>
<script type="text/javascript">
	$(function(){              	   
        
		$('#dg').edatagrid({		 
            url: '/Table/json?table=<?php echo $this->table; ?>'
		});
	});
    
	var Table = {
		add: function add(){
			$('#dlg').dialog('open').dialog('setTitle','<?php echo $this->settings['addTitle']; ?>');
			$('#fm').form('clear');
			Table.mode = 'add';
		},
		edit: function() {
			var row = $('#dg').datagrid('getSelected');
			$('#fm').form('clear');
			var rowData = {};
			if (row){
    		    var isCat = row.isCat;
    			if(isCat == 1)
    			{
    				$('#isCat').attr('checked','checked');
    			}
    			else
    			{
    				$('#isCat').removeAttr('checked');
    			}
				$('#dlg').dialog('open').dialog('setTitle','<?php echo $this->settings['editTitle']; ?>');
				for(key in row) {
					rowData['data[' + key + ']'] = row[key];
				}
				$('#fm').form('load',rowData);
			}
			Table.mode = 'edit';
			Table.id = row.id;
		},
		save: function() {
			var url = '';
			switch(Table.mode) {
				case 'add' : 
					url = 'Table/add?table=<?php echo $this->table; ?>';
					break;
				case 'edit' : 
					url = 'Table/edit?table=<?php echo $this->table; ?>&id=' + Table.id;
					break;
			}
			$('#fm').form('submit',{
				url: url,
				onSubmit: function(){
					return $(this).form('validate');
				},
				success: function(result){
					var result = eval('('+result+')');
                    console.log(result);
					if (result.success){
						$('#dlg').dialog('close');		
						$('#dg').datagrid('reload');	
					} else {
						$.messager.show({
							title: 'Error',
							msg: result.msg
						});
					}
				}
			});
		}
	};
    function del(){
		var row = $('#dg').datagrid('getSelected');
		if (row){
			$.messager.confirm('Thông báo','Bạn có chắc chắn muốn xóa?',function(r){
				if (r){
					$.post('Table/del?table=<?php echo $this->table; ?>&',{id:row.id},function(result){
						if (result.success){
							$('#dg').datagrid('reload');	
						} else {
							$.messager.show({	
								title: 'Error',
								msg: result.msg
							});
						}
					},'json');
					$('#dg').datagrid('reload');
				}
			});
		}else{
			$.messager.show({
				title:'Thông báo',
				msg:'Không có bản ghi nào được chọn!',
				timeout:5000,
				showType:'slide'
			});
		}
	}
      
</script>
<!-- Begin Datagrid -->
 <div style="height:450px">    
<table id="dg" title="<?php echo $this->settings['title'] ?>" style="width: 100%;"
	toolbar="#toolbar" pagination="true" idField="id" rownumbers="true"
	fitColumns="false" singleSelect="true" nowrap="true"
	data-options="border:false,fit:true"  treeField="name">
	<thead>
		<tr>
			<?php foreach($this->settings['fields'] as $field) { ?>
			<th field="<?php echo $field['name']?>" width="<?php echo $field['width']?>" sortable="<?php echo $field['sortable']?>" formatter="<?php echo $field['formatter']?>"><?php echo $field['title']?></th>
			<?php }?>
			<th field="id" data-options="hidden:true">Id</th>
		</tr>
	</thead>
</table>
</div>
<div id="toolbar" style="padding: 5px; height: auto;">
	<a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true"
		onclick="Table.add()">Thêm</a> <a href="#" class="easyui-linkbutton"
		iconCls="icon-edit" plain="true" onclick="Table.edit()">Sửa</a> <a href="#"
		class="easyui-linkbutton" iconCls="icon-remove" plain="true"
		onclick="del()">Xóa</a>
 <?php if($this->table=='user' || $this->table=='role' ){ ?>
    <div style="float: right;">  
        <a href="/Table?table=user" class="easyui-linkbutton catalog" iconCls="icon-redo" plain="true" >Danh sách user</a>
        <a href="/Table?table=role" class="easyui-linkbutton catalog" iconCls="icon-redo" plain="true"  >Chức vụ</a>       
        <a href="/Catalog" class="easyui-linkbutton catalog" iconCls="icon-redo" plain="true" >Bảng tính lương</a>    
    </div> 
 <?php }?> 
	<!-- Begin Search 
	<div id="Sear" style="padding: 3px">
		<span>Tên người dùng:</span> <input id="sUser"
			style="line-height: 18px; border: 1px solid #ccc"> <span>Tên đơn vị:</span>
		<input id="sTentruong"
			style="line-height: 18px; border: 1px solid #ccc"> <span>Địa chỉ:</span>
		<input id="sAddress" style="line-height: 18px; border: 1px solid #ccc">
		<a href="#" class="easyui-linkbutton" iconCls="icon-search"
			plain="true" onclick="Table.search()">Tìm kiếm</a>
	</div>
	 End Search -->
</div>

<!-- End DataGrid -->

<!-- Begin Form -->
<div id="dlg" class="easyui-dialog"
	style="width: 600px; height: 320px; background: #e7e7ef; padding: 10px;"
	closed="true" buttons="#dlg-buttons">
	<form id="fm" method="post" enctype="multipart/form-data">
		<table border="0" width="auto" cellspacing="0" cellpadding="4"
			class="tblForm">
			<?php foreach($this->settings['editing']['elements'] as $elem) {?>
			<tr>
				<td width="100px;">
					<div><?php echo $elem['title']?>:</div>
				</td>
				<td><input  id="<?php echo $elem['name'] ;?>"  name="data[<?php echo $elem['name'];?>]"
					 data-options="<?php echo $elem['data-options'];?>"  class="<?php echo $elem['class'];?>" <?php if(@$elem['url']) {?>url="<?php echo @$elem['url'];?>" <?php } ?> <?php if(@$elem['valueField']) {?>valueField="<?php echo @$elem['valueField'];?>" <?php }?>
							<?php if(@$elem['textField']) {?> textField="<?php echo @$elem['textField']?>"<?php }?> value="<?php echo @$elem['value'];?>"
					style="width: <?php echo $elem['width'];?>" type="<?php echo $elem['type'];?>" /></td>
			</tr>	
			<?php }?> 
                     
            
		</table>
	</form>
</div>
<div id="dlg-buttons">
	<a href="#" class="easyui-linkbutton" iconCls="icon-ok"
		onclick="Table.save()">Câp nhật</a> <a href="#" class="easyui-linkbutton"
		iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')">Hủy</a>
</div>
<!-- End From -->
